﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Tradex.Messages;

namespace Tradex.Trading {
    
    /// <summary>
    /// The Position classis the dynamic aggregator for information about a position.
    /// A position is quite specific (bound to an account, a trade component). As a result,
    /// quite a number of different positions can / may exist.
    /// </summary>
    public sealed class Position : ComponentModel.DynamicObject {

        /// <summary>
        /// The public constructor taking a PositionKey as parameter.
        /// </summary>
        /// <param name="key"></param>
        public Position(PositionKey key) {
            Key = key;
        }

        /// <summary>
        /// Gets the Positionkey.
        /// </summary>
        public PositionKey Key { get; private set; }

        /// <inheritdoc />
        public override void ProcessMessage(Message message) {
            if (message == null) {
                return;
            }
            switch (message.Code) {
                case MessageCode.PositionUpdate:
                    PositionUpdateMessage updateMessage = (PositionUpdateMessage) message;
                    if (updateMessage.Update == null) {
                        break;
                    }
                    if (_Update == null || updateMessage.Update.Timestamp > _Update.Timestamp) {
                        Update = updateMessage.Update;
                    }
                    break;
            }
        }

        /// <summary>
        /// Gets the Update.
        /// </summary>
        public PositionUpdate Update {
            get {
                lock (this) {
                    return _Update;
                }
            }
            private set {
                lock (this) {
                    OnPropertyChanged("Info", ref _Update, value);
                }
            }
        }
        PositionUpdate _Update;

    }
}
